// merchants/projectDetail/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carditemId: 0,//消费卡id
    userProjectList: [],
    currentPage: 1,
    loadingMoreHidden: 1,
    cardType: 0,
    addMoney: 0,
    balance: 0
  },

  // 查看已开通当前卡的会员
  getCardItemUserList: function(id) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCardUserList',
      data: {
        cardItemId: id,
        page: that.data.currentPage
      },
      method: 'POST',
      success: function(res) {
        if (res.data.Code == 0) {
          let projectInfo = res.data.Data.List;
          if (that.data.currentPage != 1) {
            var preGoods = that.data.userProjectList
            projectInfo = preGoods.concat(projectInfo);
          }
          for (let i = 0; i < projectInfo.length; i++) {
            projectInfo[i].Days = that.dateMinus(projectInfo[i].AddTime, projectInfo[i].ExpireTime);
            let year = projectInfo[i].ExpireTime.substr(0, 4);
            let month = projectInfo[i].ExpireTime.substr(5, 2);
            let day = projectInfo[i].ExpireTime.substr(8, 2)
            projectInfo[i].ExpireTime = year + '年' + month + '月' + day + '日';
          }
          that.setData({
            userProjectList: projectInfo,
            loadingMoreHidden: res.data.Data.PageModel.TotalPages
          })

        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  // 查看单张卡的统计信息
  getCardItemTotal: function(id, cardtype) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCardItemTotal?cardItemId=' + id + '&cardType=' + cardtype,
      success: function(res) {
        if (res.data.Code == 0) {
          if (that.data.cardType == 1) {
            wx.setNavigationBarTitle({
              title: '储值卡(' + res.data.Data.CardCount + '张)'
            })
          }
          if (that.data.cardType == 2) {
            wx.setNavigationBarTitle({
              title: '次卡(' + res.data.Data.CardCount + '张)'
            })
          }
          if (that.data.cardType == 3) {
            wx.setNavigationBarTitle({
              title: '时效卡(' + res.data.Data.CardCount + '张)'
            })
          }
          that.setData({
            addMoney: res.data.Data.AddMoney,
            balance: res.data.Data.Balance
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })

        }
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.loadMore();
  },

  //计算日期相减天数 
  dateMinus: function(sDate, eDate){
    let sdate = new Date(sDate.replace(/-/g, "/"));
    let edate = new Date(eDate.replace(/-/g,"/"));
    let days = edate.getTime() - sdate.getTime();
    let day = parseInt(days / (1000 * 60 * 60 * 24));
    return day;
  },
    
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      cardType: options.cardtype
    })
    if (options.id) {
      this.getCardItemUserList(options.id);
      this.getCardItemTotal(options.id, options.cardtype);
      this.setData({
        carditemId: options.id
      })
    }
    if (options.cardtype == 1) {
      wx.setNavigationBarTitle({
        title: '储值卡'
      })
    }
    if (options.cardtype == 2) {
      wx.setNavigationBarTitle({
        title: '次卡'
      })
    }
    if (options.cardtype == 3) {
      wx.setNavigationBarTitle({
        title: '时效卡'
      })
    }

  },

  // 加载更多
  loadMore: function (e) {
    var that = this;
    if (that.data.loadingMoreHidden > 1 && that.data.currentPage < that.data.loadingMoreHidden) {
      var currPage = that.data.currentPage;
      that.setData({
        currentPage: currPage + 1
      })
      that.getCardItemUserList(that.data.carditemId);
    } else {
      return
    }
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },


})